import time

import requests
from fake_useragent import UserAgent
import csv

ua = UserAgent()
for i in range(1,1642):
    params = {
        'dataType': 'ZQFXLISTBYAD',
        'adCode': '87',
        'page': f'{i}',
        'pageSize': '10',
    }
    headers = {
        'User-Agent': ua.random
    }
    url = 'https://www.governbond.org.cn:4443/api/loadBondData.action?timeStamp=1744284092347&dataType=ZQFXLISTBYAD&adList=&adCode=87&zqlx=&year=&fxfs=&qxr=&fxqx=&zqCode=&zqName=&page=1&pageSize=10'
    response = requests.get(url, params=params, headers=headers)
    print(response.json())
    data = []
    print(f'正在爬取第{i}页')
    for item in response.json()['data']:
        ZQ_NAME = item['ZQ_NAME']
        ZQ_CODE = item['ZQ_CODE']
        AD_NAME = item['AD_NAME']
        ZQ_FXTIME = item['ZQ_FXTIME']
        ZQQX_NAME = item['ZQQX_NAME'].strip()
        LXFS_ID = item['LXFS_ID']
        LL = item['LL']
        FXFS = item['FXFS']
        QX_DATE = item['QX_DATE']
        data.append([ZQ_NAME, ZQ_CODE, AD_NAME, ZQ_FXTIME, ZQQX_NAME, LXFS_ID, LL, FXFS, QX_DATE])
        time.sleep(1)

        # print(ZQ_NAME,ZQ_CODE,AD_NAME,ZQ_FXTIME,ZQQX_NAME,LXFS_ID,LL,FXFS,QX_DATE)
        with open('地方债.csv', 'w', encoding='utf-8', newline='') as f:
            writer = csv.writer(f)
            writer.writerow(['债卷名', '债卷代码', '地区', '发行时间', '发行期限', '利率', '付息方式', '到期日期'])
            writer.writerows(data)

